package com.example.vae.Home.Invitation;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.vae.Activity.FabuActivity;
import com.example.vae.Adapter.MyFragmentPageAdapter;
import com.example.vae.Home.Invitation.invitation_three.guangchangFragment;
import com.example.vae.Home.Invitation.invitation_three.guanzhuFragment;
import com.example.vae.Home.Invitation.invitation_three.remenFragment;
import com.example.vae.InvitationViewModel;
import com.example.vae.R;
import com.example.vae.databinding.InvitationFragmentBinding;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class InvitationFragment extends Fragment {

    private InvitationViewModel mViewModel;
    private InvitationFragmentBinding binding;

    private TabLayout tabLayout;
    private ViewPager vp1;
    private List<Fragment> list=new ArrayList<>();
    private MyFragmentPageAdapter fragmentPagerAdapter;
    private String [] titles={"广场","热门","关注"};
    public static InvitationFragment newInstance() {
        return new InvitationFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //       绑定首页面的数据
        mViewModel = ViewModelProviders.of(requireActivity()).get(InvitationViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.invitation_fragment, container, false);
        binding.setData(mViewModel);
//        设置数据变化 的观察
        binding.setLifecycleOwner(requireActivity());

        tabLayout=binding.tab;
        vp1=binding.vp1;
        list.add(new guangchangFragment());
        list.add(new remenFragment());
        list.add(new guanzhuFragment());

        fragmentPagerAdapter=new MyFragmentPageAdapter(getFragmentManager(),0,list,titles);
        vp1.setAdapter(fragmentPagerAdapter);
        tabLayout.setupWithViewPager(vp1);
        tabLayout.getTabAt(0).select();

        binding.fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), FabuActivity.class);
                startActivity(intent);
            }
        });

       return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mViewModel = ViewModelProviders.of(this).get(InvitationViewModel.class);
        // TODO: Use the ViewModel
    }

}
